#include <iostream>
using namespace std;

typedef long long ll;
ll n, a1, m1, a2, m2;
ll k1, k2;
ll d;
ll k;
ll x = 0;

ll exgcd(ll a, ll b, ll &x, ll &y)
{
    if (!b)
    {
        x = 1;
        y = 0;
        return a;
    }
    ll x0, y0;
    ll d = exgcd(b, a % b, x0, y0);
    x = y0;
    y = x0 - a / b * y0;
    return d;
}

int main()
{
    cin >> n;
    cin >> a1 >> m1;
    n--;
    while (n--)
    {
        cin >> a2 >> m2;
        d = exgcd(a1, a2, k1, k2);
        if (abs(m1 - m2) % d)
        {
            x = -1;
            break;
        }
        k1 = k1 * abs(m1 - m2) / d;
        k = a2 / d;
        k1 = (k1 % k + k) % k;
        x = k1 * a1 + m1;
        m1 = x;
        a1 = a1 * a2 / d;
    }
    cout << x;
    return 0;
}